package com.hpjpw.idoapp.service;

import com.hpjpw.ido.domain.Task;
import com.hpjpw.ido.service.ITaskService;
import com.hpjpw.idoapp.domain.TaskImportVo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TaskImportService {
    @Autowired
    ITaskService taskService;

    /* 创建一条任务数据 */
    public boolean createTask(TaskImportVo taskImport){
        if (StringUtils.isNull(taskImport)){
            return false;
        }
        Task task = new Task();
        task.setName(taskImport.getName());
        task.setStar(taskImport.getStar());
        task.setDescribe(taskImport.getDescribe());
        taskService.insertTask(task);
        return true;
    }


    /* 导入任务 */
    public String importTask(List<TaskImportVo> taskList){
        /* 判断导入总表是否为空 */
        if (StringUtils.isNull(taskList)||taskList.size() == 0){
            throw new CustomException("导入实习宝藏任务数据不能为空！");
        }
        /* 表格中成功导入与失败导入条数 */
        int successNum = 0;
        int failureNum = 0;
        /* 成功导入以及失败导入的消息提示 */
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        /* 对每一条导入任务数据进行校验 */
        for (TaskImportVo item:taskList) {
            if (createTask(item)){
                successNum++;
                String msg = "<br/>" + successNum + "：任务名称 " + item.getName() + " 导入成功！";
                successMsg.append(msg);
            }else {
                failureNum++;
                String msg = "<br/>" + failureNum + "：任务名称 " + item.getName() + " 导入失败！";
                failureMsg.append(msg);
            }
        }
        return successMsg.toString();
    }
}
